package Notes;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import java.awt.GridBagLayout;

import javax.swing.JTextField;

import java.awt.GridBagConstraints;
import java.awt.Insets;

import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;

import javax.swing.JLabel;

import java.awt.Font;
import java.util.Arrays;

public class Lotto1 extends JFrame {
	
	int[] lotto = new int[6];
	
	private JPanel contentPane;
	private JButton btnNewButton;
	private JLabel lblNewLabel;
	private JLabel label;
	private JLabel label_1;
	private JLabel label_2;
	private JLabel label_3;
	private JLabel label_4;
	private JLabel lblNewLabel_1;
	private JLabel lblNewLabel_2;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Lotto1 frame = new Lotto1();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Lotto1() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		GridBagLayout gbl_contentPane = new GridBagLayout();
		gbl_contentPane.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0, 0};
		gbl_contentPane.rowHeights = new int[]{87, 23, 83, 0};
		gbl_contentPane.columnWeights = new double[]{1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, Double.MIN_VALUE};
		gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
		contentPane.setLayout(gbl_contentPane);
		GridBagConstraints gbc_lblNewLabel_2 = new GridBagConstraints();
		gbc_lblNewLabel_2.gridwidth = 7;
		gbc_lblNewLabel_2.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_2.gridx = 0;
		gbc_lblNewLabel_2.gridy = 0;
		contentPane.add(getLblNewLabel_2(), gbc_lblNewLabel_2);
		GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
		gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel.gridx = 0;
		gbc_lblNewLabel.gridy = 1;
		contentPane.add(getLblNewLabel(), gbc_lblNewLabel);
		GridBagConstraints gbc_label = new GridBagConstraints();
		gbc_label.insets = new Insets(0, 0, 5, 5);
		gbc_label.gridx = 1;
		gbc_label.gridy = 1;
		contentPane.add(getLabel(), gbc_label);
		GridBagConstraints gbc_label_1 = new GridBagConstraints();
		gbc_label_1.insets = new Insets(0, 0, 5, 5);
		gbc_label_1.gridx = 2;
		gbc_label_1.gridy = 1;
		contentPane.add(getLabel_1(), gbc_label_1);
		GridBagConstraints gbc_label_2 = new GridBagConstraints();
		gbc_label_2.insets = new Insets(0, 0, 5, 5);
		gbc_label_2.gridx = 3;
		gbc_label_2.gridy = 1;
		contentPane.add(getLabel_2(), gbc_label_2);
		GridBagConstraints gbc_label_3 = new GridBagConstraints();
		gbc_label_3.insets = new Insets(0, 0, 5, 5);
		gbc_label_3.gridx = 4;
		gbc_label_3.gridy = 1;
		contentPane.add(getLabel_3(), gbc_label_3);
		GridBagConstraints gbc_label_4 = new GridBagConstraints();
		gbc_label_4.insets = new Insets(0, 0, 5, 5);
		gbc_label_4.gridx = 5;
		gbc_label_4.gridy = 1;
		contentPane.add(getLabel_4(), gbc_label_4);
		GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
		gbc_btnNewButton.insets = new Insets(0, 0, 5, 0);
		gbc_btnNewButton.gridx = 6;
		gbc_btnNewButton.gridy = 1;
		contentPane.add(getBtnNewButton(), gbc_btnNewButton);
		GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
		gbc_lblNewLabel_1.insets = new Insets(0, 0, 0, 5);
		gbc_lblNewLabel_1.gridx = 0;
		gbc_lblNewLabel_1.gridy = 2;
		contentPane.add(getLblNewLabel_1(), gbc_lblNewLabel_1);
		setTitle("로또 프로그램");
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("로또 번호");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
					String str=JOptionPane.showInputDialog(btnNewButton,"담청되면 반띵!!! 주실거죠?","당첨되면 반띵!!!",JOptionPane.INFORMATION_MESSAGE);
					//System.out.println(str);
					
					
					
					int kakunin=JOptionPane.showConfirmDialog(btnNewButton,
							"담청 되면 반띵!!! 주길거임?",//창 내용
							"담청되면 반띵!!!",//타이틀
							JOptionPane.OK_CANCEL_OPTION,//확인 취소 옵션
							JOptionPane.QUESTION_MESSAGE
							);
					//System.out.println(kakunin);//확인=0, 취소=2;
					
					if(kakunin==0){
						random(1,45,6,lotto);//1부터 45까지의 숫자 중 아무거나 6개를 lotto라는 배열에 담음
						Arrays.sort(lotto);//배열을 오름차순으로 바꿈
						
						lblNewLabel.setText(Integer.toString(lotto[0]));
						label.setText(Integer.toString(lotto[1]));
						label_1.setText(Integer.toString(lotto[2]));
						label_2.setText(Integer.toString(lotto[3]));
						label_3.setText(Integer.toString(lotto[4]));
						label_4.setText(Integer.toString(lotto[5]));
						JOptionPane.showMessageDialog(btnNewButton,"당첨되면 반띵!!!","반띵!!!",JOptionPane.WARNING_MESSAGE);
					}
					else{
						
						JOptionPane.showMessageDialog(btnNewButton,"안줄거면 꺼져!!!","꺼져",JOptionPane.OK_OPTION);
					}
					
				}
			});
		}
		return btnNewButton;
	}
	
	public static void random(int hajime, int owari, int kosuu, int[] lotto){
		int pos=0;//배열에 담기 위해 차수를 확인하기 위한 변수 
		boolean flag=false;//랜덤으로 발생한 수가 배열에 있는지 확인을 위한 변수
		while(pos<kosuu){
			int random = (int)(Math.random()*(owari-hajime+1))+hajime;//랜덤으로 발생 한 수
			for(int i=0; i<lotto.length;i++){
				if(random==lotto[i]){//랜덤으로 발생한 수가 배열에 있다면
					flag = true;
					break;
				}
			}
			if(!flag){//랜덤으로 발생한 수가 배열에 없다면
				lotto[pos]=random;//배열에 랜덤으로 발생 한 수를 집어 넣음
				pos++;//차수 증가
			}
			flag=false;
		}
	}
	
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("1");
			lblNewLabel.setFont(new Font("굴림", Font.BOLD, 40));
			lblNewLabel.setForeground(Color.RED);
			lblNewLabel.setBackground(Color.WHITE);
		}
		return lblNewLabel;
	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("2");
			label.setForeground(Color.BLUE);
			label.setFont(new Font("굴림", Font.BOLD, 40));
			label.setBackground(Color.WHITE);
		}
		return label;
	}
	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("3");
			label_1.setForeground(Color.MAGENTA);
			label_1.setFont(new Font("굴림", Font.BOLD, 40));
			label_1.setBackground(Color.WHITE);
		}
		return label_1;
	}
	private JLabel getLabel_2() {
		if (label_2 == null) {
			label_2 = new JLabel("4");
			label_2.setForeground(Color.CYAN);
			label_2.setFont(new Font("굴림", Font.BOLD, 40));
			label_2.setBackground(Color.WHITE);
		}
		return label_2;
	}
	private JLabel getLabel_3() {
		if (label_3 == null) {
			label_3 = new JLabel("5");
			label_3.setForeground(Color.GREEN);
			label_3.setFont(new Font("굴림", Font.BOLD, 40));
			label_3.setBackground(Color.WHITE);
		}
		return label_3;
	}
	private JLabel getLabel_4() {
		if (label_4 == null) {
			label_4 = new JLabel("6");
			label_4.setForeground(Color.DARK_GRAY);
			label_4.setFont(new Font("굴림", Font.BOLD, 40));
			label_4.setBackground(Color.WHITE);
		}
		return label_4;
	}
	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("");
		}
		return lblNewLabel_1;
	}
	private JLabel getLblNewLabel_2() {
		if (lblNewLabel_2 == null) {
			lblNewLabel_2 = new JLabel("당첨되면 반띵");
			lblNewLabel_2.setForeground(Color.RED);
			lblNewLabel_2.setFont(new Font("굴림", Font.BOLD, 30));
		}
		return lblNewLabel_2;
	}
}
